package com.taikang.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.taikang.base.page.PageResult;
import com.taikang.system.dao.ISysRegionDao;
import com.taikang.system.dto.SysRegionCondition;
import com.taikang.system.entity.SysRegion;
import com.taikang.system.service.ISysRegionService;

@Service("sysRegionServiceImpl")
public class SysRegionServiceImpl implements ISysRegionService{

	@Resource(name = "sysRegionDaoImpl")
	private ISysRegionDao sysRegionDao;
	
	/**
	 * 查询所有省份
	 * @return
	 */
	public List<SysRegion> listProvinceAll(){
		return sysRegionDao.listProvinceAll();
	}

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listSysRegionByPage(PageResult<SysRegion> pageResult,SysRegionCondition condition){
		int rows = sysRegionDao.listSysRegionByPageCount(condition);
		pageResult.setRows(rows);
		
		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<SysRegion> list = sysRegionDao.listSysRegionByPage(rowBounds,condition);
		pageResult.setResult(list);
	}
	
	/**
	 * 查询所有区域
	 * @return
	 */
	public List<SysRegion> listSysRegionAll(){
		return sysRegionDao.listSysRegionAll();
	}
	
	/**
	 * 查询适应门店
	 */
	public List<SysRegion> listSysRegionsByCondition(SysRegionCondition condition) {
		return sysRegionDao.listSysRegionsByCondition(condition);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 省市区基础表
	 */
	public SysRegion getSysRegionById(Long id){
		return sysRegionDao.getSysRegionById(id);
	}

	/**
	 * 新增
	 * @param item 省市区基础表
	 */
	public void add(SysRegion item){
		if (null == item.getSort()) {
			item.setSort(0);
		}
		if (null == item.getAreaId()) {
			item.setAreaId("0");
		}
		sysRegionDao.add(item);
	}

	/**
	 * 修改
	 * @param item 省市区基础表
	 */
	public void update(SysRegion item){
		sysRegionDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(Long[] ids){
		sysRegionDao.delByIds(ids);
	}

	/**
	 * 设置热门
	 */
	public void setHot(Long[] ids,Integer status) {
		sysRegionDao.setHot(ids,status);
		List<Long> parentIdsList = Arrays.asList(ids);
		while(true){
			List<Long> childrenIdList = sysRegionDao.listSysRegionByParentId(parentIdsList);
			if (CollectionUtils.isEmpty(childrenIdList)) {
				break;
			}
			sysRegionDao.setHot(childrenIdList.toArray(new Long[childrenIdList.size()]), status);
			parentIdsList = new ArrayList<Long>();
			parentIdsList.addAll(childrenIdList);
		}
	}

	/**
	 * 分页查询大区的详情
	 */
	public void listSysRegionAreaDetailByPage(PageResult<SysRegion> pageResult,SysRegionCondition condition) {
		int rows = sysRegionDao.listSysRegionAreaDetailByPageCount(condition);
		pageResult.setRows(rows);
		
		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<SysRegion> list = sysRegionDao.listSysRegionAreaDetailByPage(rowBounds,condition);
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getCode().length()==4 || list.get(i).getCode().length()==6 && list.get(i).getLevel()==2){
				if(list.get(i).getCode().length()==4){
					SysRegion sysRegion = sysRegionDao.getSysRegionByCode(list.get(i).getCode().substring(0,2));
					list.get(i).setCityName(sysRegion.getName());
				}
				if(list.get(i).getCode().length()==6 && list.get(i).getLevel()==3){
					SysRegion sysRegion = sysRegionDao.getSysRegionByCode(list.get(i).getCode().substring(0,4));
					list.get(i).setCityName(sysRegion.getName());
				}
			}
			if(list.get(i).getCode().length()==6 && list.get(i).getLevel()==3){
				SysRegion sysRegion2 = sysRegionDao.getSysRegionByCode(list.get(i).getCode().substring(0,2));
				SysRegion sysRegion = sysRegionDao.getSysRegionByCode(list.get(i).getCode().substring(0,4));
				list.get(i).setCityName(sysRegion2.getName()+" / "+sysRegion.getName());
			}
		}
		pageResult.setResult(list);
	}
	
	/**
	 * 批量设置区域
	 */
	public void batchFitArea(Long[] ids, String areaId) {
		sysRegionDao.batchFitArea(ids, areaId);
		List<Long> parentIdsList = Arrays.asList(ids);
		while(true){
			List<Long> childrenIdList = sysRegionDao.listSysRegionByParentId(parentIdsList);
			if (CollectionUtils.isEmpty(childrenIdList)) {
				break;
			}
			sysRegionDao.batchFitArea(childrenIdList.toArray(new Long[childrenIdList.size()]), areaId);
			parentIdsList = new ArrayList<Long>();
			parentIdsList.addAll(childrenIdList);
		}
	}

	/**
	 * 设置区域
	 */
	public void fitArea(SysRegionCondition condition) {
		sysRegionDao.fitArea(condition);
		List<Long> parentIdsList = new ArrayList<Long>();
		parentIdsList.add(condition.getRegionId());
		while(true){
			List<Long> childrenIdList = sysRegionDao.listSysRegionByParentId(parentIdsList);
			if (CollectionUtils.isEmpty(childrenIdList)) {
				break;
			}
			sysRegionDao.batchFitArea(childrenIdList.toArray(new Long[childrenIdList.size()]), condition.getAreaId());
			parentIdsList.clear();
			parentIdsList.addAll(childrenIdList);
		}
	}

	/**
	 * 移除设置的区域
	 */
	public void removeArea(SysRegionCondition condition) {
		sysRegionDao.removeArea(condition);
		List<Long> parentIdsList = new ArrayList<Long>();
		parentIdsList.add(condition.getRegionId());
		while(true){
			List<Long> childrenIdList = sysRegionDao.listSysRegionByParentId(parentIdsList);
			if (CollectionUtils.isEmpty(childrenIdList)) {
				break;
			}
			sysRegionDao.batchRemoveArea(childrenIdList.toArray(new Long[childrenIdList.size()]));
			parentIdsList.clear();
			parentIdsList.addAll(childrenIdList);
		}
	}

	/**
	 * 批量的移除设置的区域
	 */
	public void batchRemoveArea( Long[] ids) {
		sysRegionDao.batchRemoveArea(ids);
		List<Long> parentIdsList = Arrays.asList(ids);
		while(true){
			List<Long> childrenIdList = sysRegionDao.listSysRegionByParentId(parentIdsList);
			if (CollectionUtils.isEmpty(childrenIdList)) {
				break;
			}
			sysRegionDao.batchRemoveArea(childrenIdList.toArray(new Long[childrenIdList.size()]));
			parentIdsList = new ArrayList<Long>();
			parentIdsList.addAll(childrenIdList);
		}
		
	}
	/**
	 * 通过父类条件得到子类的数据
	 */
	public List<SysRegion> listSysregionByCondition(SysRegionCondition condition) {
		return sysRegionDao.listSysregionByCondition(condition);
	}

	@Override
	public List<SysRegion> listSysRegionByAreaId(String areaId) {
		return sysRegionDao.listSysRegionByAreaId(areaId);
	}

	@Override
	public SysRegion getSysRegionByCode(String code) {
		return sysRegionDao.getSysRegionByCode(code);
	}

	@Override
	public SysRegion getSysRegionByName(String name) {
		return sysRegionDao.getSysRegionByName(name);
	}

	@Override
	public SysRegion getSysRegionByAllName(String name) {
		return sysRegionDao.getSysRegionByAllName(name);
	}

}

